# -*- coding: utf-8 -*-

from scrapy import Request
import hashlib
import pymongo
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.pipelines.images import ImagesPipeline

s1=hashlib.sha1()
client = pymongo.MongoClient('mongodb://ali2.liushiqi.top:27017')

mill_ = client['mill']
tb=mill_['notebook']

class CarPipeline(object):
    def process_item(self, item, spider):
        print(item)
        return item


class DownloadImagesPipeline(ImagesPipeline):
    default_headers = {
        'accept': 'image/webp,image/*,*/*;q=0.8',
        'accept-encoding': 'gzip, deflate, sdch, br',
        'accept-language': 'zh-CN,zh;q=0.8,en;q=0.6',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
    }


    def item_completed(self, results, item, info):
        # [(True,
        #   {'url': 'https://ke-image.ljcdn.com/newhouse-baichuan-image/5e163ebb07abb5c82c256a8acc674851.jpg.592x432.jpg',
        #    'path': 'full/85378bfd92511e8bb16461b26ed1af1a42bf5a27.jpg',
        #    'checksum': '746f3b57cc561af6df769ab641717d8a'})]
        # item['imgn']=results[0][1]['path']
        # shaoyang.insert_one(item)
        if results:
            item['local_main_img']=results[0][1].get('path')
        item['local_imgs']=[]
        for x in results[1:]:
            item['local_imgs'].append(x[1].get('path'))
        tb.insert_one(item)
        return item

    def get_media_requests(self, item, info):  # 下载图片

        s1.update(item['main_img'].encode('utf-8'))
        title_sha1=s1.hexdigest()

        yield Request(item["main_img"],meta={'title': title_sha1},headers=self.default_headers)
        for img in item['imgs']:
            s1.update(img.encode('utf-8'))
            yield Request(img, meta={'title': s1.hexdigest()}, headers=self.default_headers)
                 # 添加meta是为了下面重命名文件名使用

    def file_path(self, request, response=None, info=None):
        return request.meta['title']+'.jpg'
        pass
